import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';


class LedPage extends StatefulWidget {
  LedPage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _LedPageState createState() => _LedPageState();
}

class _LedPageState extends State<LedPage>
    with SingleTickerProviderStateMixin {
  String path = 'assets/images/common/device/d-an.png';
  String path2 = 'assets/images/common/device/d-liang.png';
  String work = '开启';
  String work2 = '熄灭';

  int state = 0;
  int state2 = 0;
  Timer _timer;
  Animation<double> animation;
  AnimationController _controller;

  @override
  void dispose() {
    _controller.stop();
    _controller.dispose();
    super.dispose();
    _timer.cancel();

    print('object');
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();



    // _getishow();
    // _getishow2();
    // const timeout = const Duration(seconds: 1);
    // print('currentTime=' + DateTime.now().toString()); // 当前时间
    // _timer = Timer(timeout, () {
    //   _getishow();
    // });



  }

  void _getishow() async {
    var response = await Http.get('/lamp/getById?id=10001');
    print(response);
    if (response['content'] == 'isshow')
      state = 1;
    else
      state = 0;
  }

  void _getishow2() async {
    Map<String, String> headers = {
      'Authorization':
          'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNzY4NDAyNTFAcXEuY29tIiwiaWF0IjoxNjI2ODU2OTIxLCJleHAiOjE2Mjc0NjE3MjF9.pGaQM2s2-UYVKpw-s1yCErwYBRdchTx1TTpdOYYUSjaQmUFNZWKX7RkZJkVd3EJ9scbJscZ2nc04edH4yQ2a_A'
    };
    Http.setHeaders(headers);
    var response = await Http.get('/lamp/getById?id=10000');
    print(response);
    if (response['content'] == 'isshow')
      state2 = 1;
    else
      state2 = 0;
  }

  _turnon() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=1&deviceId=10001&operation=turnon');
    print(response);
  }

  _turnoff() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=1&deviceId=10001&operation=turnoff');
    print(response);
  }

  _turnon2() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=1&deviceId=10000&operation=turnon');
    print(response);
  }

  _turnoff2() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=1&deviceId=10000&operation=turnoff');
    print(response);
  }

  @override
  Widget build(BuildContext context) {
    String path3 = state == 1 ? path : path2;
    String work3 = state == 1 ? work : work2;
    String path3_2 = state2 == 1 ? path : path2;
    String work3_2 = state2 == 1 ? work : work2;
    return Material(
      child: Container(
        color: Color.fromRGBO(108, 127, 197, 1),
        child: Stack(
          alignment: Alignment.center,
          children: [
            // Positioned(
            //     top: 380, left: 10, child: Image.asset('images/dbj-1.png')),
            // Positioned(
            //     top: 550, left: 200, child: Image.asset('images/dbj-2.png')),
            Scaffold(
              backgroundColor: Color.fromRGBO(108, 127, 197, 1),
              appBar: AppBar(
                elevation: 0,
                backgroundColor: Color.fromRGBO(108, 127, 197, 1),
                leading: InkWell(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Icon(
                    Icons.keyboard_arrow_left,
                    color: Color.fromRGBO(255, 255, 255, 1),
                  ),
                ),
              ),
              body: Container(
                // width: 360,
                height: 640,
                // color: Colors.deepOrange,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Positioned(
                        // top: animation.value,
                        top:20,
                        child: Container(child: Image.asset('assets/images/common/device/台灯.png'))),
                    Positioned(
                      bottom: 40,
                      child: Container(
                        // width: 300,
                        height: 250,
                        decoration: new BoxDecoration(
                          // color: Color.fromRGBO(255, 255, 255, 0.7),
                          //设置四周圆角 角度
                          borderRadius: BorderRadius.all(Radius.circular(20.0)),
                        ),
                        child: Column(
                          children: [
                            Container(
                              margin: EdgeInsets.fromLTRB(8, 8, 8, 8),
                              width: 300,
                              height: 100,
                              decoration: new BoxDecoration(
                                boxShadow: [
                                  BoxShadow(
                                      color: Color.fromRGBO(255, 255, 255, 0.3),
                                      offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                      blurRadius: 20.0, //阴影模糊程度
                                      spreadRadius: 0.5 //阴影扩散程度
                                      )
                                ],
                                color: Color.fromRGBO(255, 235, 150, 1),
                                //设置四周圆角 角度
                                borderRadius:
                                    BorderRadius.all(Radius.circular(10.0)),
                                //设置四周边框
                                // border: new Border.all(width: 1, color: Colors.red),
                              ),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                children: [
                                  Image.asset(path3),
                                  Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Text(
                                        // widget.name,
                                        '室外灯光',
                                        style: TextStyle(
                                            color: Colors.black87,
                                            fontSize: 22),
                                      ),
                                      Row(
                                        children: [
                                          Text(
                                            '工作状态:',
                                            style: TextStyle(
                                                color: Colors.black87,
                                                fontSize: 14),
                                          ),
                                          Text(
                                            work3,
                                            style: TextStyle(
                                                color: Colors.black54,
                                                fontSize: 14),
                                          ),
                                        ],
                                      )
                                    ],
                                  ),
                                  CupertinoSwitch(
                                    trackColor:
                                        Color.fromRGBO(255, 246, 217, 1),
                                    activeColor:
                                        Color.fromRGBO(199, 176, 123, 1),
                                    value: this.state == 1,
                                    onChanged: (bool value) {
                                      setState(() {
                                        // this.state = value;
                                        if (value) {
                                          this.state = 1;
                                          _turnon();
                                        } else {
                                          this.state = 0;
                                          _turnoff();
                                        }
                                      });
                                    },
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.fromLTRB(8, 8, 8, 8),
                              width: 300,
                              height: 100,
                              decoration: new BoxDecoration(
                                boxShadow: [
                                  BoxShadow(
                                      color: Color.fromRGBO(255, 255, 255, 0.3),
                                      offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                      blurRadius: 20.0, //阴影模糊程度
                                      spreadRadius: 0.5 //阴影扩散程度
                                      )
                                ],
                                color: Color.fromRGBO(255, 235, 150, 1),
                                //设置四周圆角 角度
                                borderRadius:
                                    BorderRadius.all(Radius.circular(10.0)),
                                //设置四周边框
                                // border: new Border.all(width: 1, color: Colors.red),
                              ),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                children: [
                                  Image.asset(path3_2),
                                  Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Text(
                                        // widget.name,
                                        '室内灯光',
                                        style: TextStyle(
                                            color: Colors.black87,
                                            fontSize: 22),
                                      ),
                                      Row(
                                        children: [
                                          Text(
                                            '工作状态:',
                                            style: TextStyle(
                                                color: Colors.black87,
                                                fontSize: 14),
                                          ),
                                          Text(
                                            work3_2,
                                            style: TextStyle(
                                                color: Colors.black54,
                                                fontSize: 14),
                                          ),
                                        ],
                                      )
                                    ],
                                  ),
                                  CupertinoSwitch(
                                    trackColor:
                                        Color.fromRGBO(255, 246, 217, 1),
                                    activeColor:
                                        Color.fromRGBO(199, 176, 123, 1),
                                    value: this.state2 == 1,
                                    onChanged: (bool value) {
                                      setState(() {
                                        // this.state = value;
                                        if (value) {
                                          this.state2 = 1;
                                          _turnon2();
                                        } else {
                                          this.state2 = 0;
                                          _turnoff2();
                                        }
                                      });
                                    },
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }




  // _buildserachbar(String s, String v) {
  //   return Container(
  //     margin: EdgeInsets.fromLTRB(0, 0, 0, 20),
  //     width: 300,
  //     height: 80,
  //     decoration: new BoxDecoration(
  //       color: Color.fromARGB(200, 100, 90, 180),
  //       //设置四周圆角 角度
  //       borderRadius: BorderRadius.all(Radius.circular(20.0)),
  //       //设置四周边框
  //       // border: new Border.all(width: 1, color: Colors.red),
  //       boxShadow: [
  //         BoxShadow(
  //             color: Colors.black12,
  //             offset: Offset(0.0, 6.0), //阴影y轴偏移量
  //             blurRadius: 5, //阴影模糊程度
  //             spreadRadius: 1 //阴影扩散程度
  //             )
  //       ],
  //     ),
  //     child: Row(
  //       mainAxisAlignment: MainAxisAlignment.spaceAround,
  //       children: [
  //         Column(
  //           mainAxisAlignment: MainAxisAlignment.center,
  //           children: [
  //             Text(
  //               '$s',
  //               style: TextStyle(color: Colors.white, fontSize: 22),
  //             ),
  //             Text(
  //               '工作状态:working..',
  //               style: TextStyle(color: Colors.white70, fontSize: 14),
  //             ),
  //           ],
  //         ),
  //         Padding(padding: const EdgeInsets.all(1.0), child: Image.asset(v)),
  //       ],
  //     ),
  //   );
  // }
}
// // 设置四周边框
// border: new Border.all(width: 1, color: Colors.red),
// boxShadow: [
//   BoxShadow(
//       color: Colors.yellow.shade100,
//       offset: Offset(0.0, 6.0), //阴影y轴偏移量
//       blurRadius: 5, //阴影模糊程度
//       spreadRadius: 1 //阴影扩散程度
//       )
// ],
